package com.ikdong.toodle.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

public class BasicHttpClient {
	private static final String BASE_URL = "http://api.toodledo.com/2/";
	private static final HttpClient httpclient = new DefaultHttpClient();
	
	public static String get(String url, Map<String,String> params){
		String json = "{'errorCode':100,'errorDesc':'Unknown Error.'}";
		String queryStr = "";
		
		if(params!=null && params.size()>0){
			Iterator<String> ite = params.keySet().iterator();
			while(ite.hasNext()){
				String key = ite.next();
				String value = params.get(key);
				queryStr += "&"+key+"="+value;
			}
			queryStr = "?"+queryStr.substring(1, queryStr.length());
		}
		
		Log.d(Constant.TAG, "url: "+url+queryStr);
		HttpGet request = new HttpGet(url+queryStr);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		try {
			json = httpclient.execute(request, responseHandler);
		} catch (Exception e) {
			Log.e(Constant.TAG, "BasicHttpClient.get(url) error", e);
		}

		Log.d(Constant.TAG, "json: "+json);
		return json;
	}
	
	public static String post(String url, Map<String,String> params){
		String json = "{'errorCode':100,'errorDesc':'Unknown Error.'}";
		try{
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
			if(params!=null && params.size()>0){
				Iterator<String> ite = params.keySet().iterator();
				while(ite.hasNext()){
					String key = ite.next();
					String value = params.get(key);
					nameValuePairs.add(new BasicNameValuePair(key, value));
				}
			}
			
			HttpPost httppost = new HttpPost(url);
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			json = httpclient.execute(httppost,responseHandler);
		}catch(Exception e){
			Log.e(Constant.TAG, "BasicHttpClient.post()", e);
		}
		return json;
	}

	public static String getAbsoluteUrl(String relativeUrl){
		return BASE_URL + relativeUrl;
	}
	
	protected static String MD5(String md5) {
		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
			}
			return sb.toString();
		} catch (java.security.NoSuchAlgorithmException e) {
		}
		return null;
	}
	
	protected static String toUnitedCode(String str){
		String result = "";
		for (int i = 0; i < str.length(); i++){  
	          int chr1 = (char) str.charAt(i);  
	          result+="\\u" + Integer.toHexString(chr1);   
	     }
		
		return result;
	}
}
